package com.csscis.ssm.web.controller;

import com.csscis.ssm.core.feature.orm.mybatis.Page;
import com.csscis.ssm.core.util.JsonUtils;
import com.csscis.ssm.web.model.PhoneUser;
import com.csscis.ssm.web.service.PhoneUserService;
import org.apache.ibatis.annotations.Param;
import org.codehaus.jackson.JsonGenerationException;
import org.codehaus.jackson.map.JsonMappingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/message")
public class PhoneUserController {

    @Autowired
    private PhoneUserService phoneUserService;

    @RequestMapping("/start")
    public String start(){
        return "phoneUserList";
    }

    /**
     * 查询所有
     * @throws IOException
     */
    @RequestMapping("/phoneUserList")
    @ResponseBody
    public String List(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNo,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize,
            PhoneUser phoneUser,
            String finde) throws IOException {

        String result = null;
        if (finde != null && "".equals(finde)) {
            if ("".equals(phoneUser.getPuserId())) {
                phoneUser.setPuserId(null);
            }
        }
        try {
            Page<PhoneUser> page = new Page<PhoneUser>(pageNo, pageSize);
            phoneUserService.selectAndPage(page, phoneUser);
            result = JsonUtils.jsonResult(page.getResult(), page.getTotalCount());
        } catch (JsonGenerationException e) {
            e.printStackTrace();
        } catch (JsonMappingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 进入添加
     * @param request
     * @return
     */
    @RequestMapping("/doAddPhoneUser")
    public String doAddPhoneUserServer(HttpServletRequest request) {

        request.setAttribute("type", "add");
        return "phoneUserEdit";
    }

    /**
     * 进入修改
     * @param request
     * @return
     */
    @RequestMapping("/phoneUserById")
    public String PhoneUserById(Long id, HttpServletRequest request) {
        PhoneUser phoneUser = phoneUserService.selectById(id);
        request.setAttribute("type", "update");
        request.setAttribute("phoneUser", phoneUser);
        return "phoneUserEdit";
    }

    /**
     * 增加操作
     * @param
     * @return
     */
    @RequestMapping("/phoneUserAdd")
    public String PhoneUserAdd(PhoneUser phoneUser, HttpServletRequest request) {
        try {
            phoneUserService.insert(phoneUser, request);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "phoneUserList";
    }

    /**
     * 修改操作
     * @param
     * @return
     */
    @RequestMapping("/phoneUserUpload")
    public String PhoneUserUpload(PhoneUser phoneUser) {
        phoneUserService.update(phoneUser);
        return "phoneUserList";
    }

    /**
     * 删除操作
     * @param id
     * @return
     */
    @RequestMapping("/phoneUserDel")
    public String PhoneUserDel(String id) {

        String[] ids = id.split(",");
        if (ids.length != 0) {
            for (String s : ids) {
                phoneUserService.delete(Long.valueOf(s));
            }
        }
        return "phoneUserList";
    }

    /**
     * 名称唯一性验证
     * @param model 对象
     * @param response
     */
    @RequestMapping(value = "/phoneUserNameOnly")
    public void selectByModel(@Param("id") Long id, PhoneUser model, HttpServletResponse response) {

        //selectAll 方法要写相应的条件
        List<PhoneUser> list = phoneUserService.selectAll(model);
        List<String> lists = new ArrayList<String>();
        for (int i = 0; i < list.size(); i++) {
            PhoneUser phoneUser = list.get(i);
            if (phoneUser.getId() != id) {
                lists.add(i, phoneUser.getPuserName());
            }
        }
        JsonUtils.checkResult(lists, response);
    }
}
